package fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.qianfeng.lingshijia.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.ExpandableAdapter;
import adapter.ShoppingSortLvAdapter;
import utils.ShoppingCart;
import utils.ShoppingGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShoppingSortFragment extends BaseFragment implements ExpandableAdapter.OnselectChangeListenner {


    ExpandableListView expandable_lv;
    ExpandableAdapter adapter;
    ImageView ivShoppingCart;
    float countCost = 0;
    TextView totalCost;

    Map<String, List<ShoppingCart>> child;
    List<ShoppingGroup> parent;
    CheckBox all_cb;

    public ShoppingSortFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_shopping_sort, container, false);
    }

    @Override
    public void init(View view) {
        expandable_lv = (ExpandableListView) view.findViewById(R.id.expandable_LV);
        adapter = new ExpandableAdapter(getActivity());
        expandable_lv.setAdapter(adapter);
        expandable_lv.setGroupIndicator(null);
        adapter.setOnselectChangeListenner(this);
        ivShoppingCart = (ImageView) view.findViewById(R.id.ivShoppingCart);
        all_cb = (CheckBox) view.findViewById(R.id.cbGoodsSelect);
        totalCost = (TextView) view.findViewById(R.id.totalCost);
    }

    @Override
    public void loadDatas() {
        ivShoppingCart.setVisibility(View.GONE);


        parent = new ArrayList<>();
        ShoppingGroup group = new ShoppingGroup();
        group.setStoreName("一号店");
        parent.add(group);
        group = new ShoppingGroup();
        group.setStoreName("天猫");
        parent.add(group);
        group = new ShoppingGroup();
        group.setStoreName("京东");
        parent.add(group);

        List<ShoppingCart> list = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            ShoppingCart cart = new ShoppingCart();
            cart.setPic_url("http://img01.taotaosou.cn/size/image15/M08/06/4C/wKgDt1XulU0IAAAAAAB4pcvWN8sAAJ1zwFzjwIAAHi9198.jpg");
            cart.setStock(i);
            cart.setStore("hahahhahah");
            cart.setProductID(i + 100);
            cart.setPrice("100.30");
            cart.setProductName("爱吃爱吃");
            list.add(cart);
        }
        list.add(new ShoppingCart());
        child = new HashMap<>();
        child.put("一号店", list);
        List<ShoppingCart> list2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ShoppingCart cart = new ShoppingCart();
            cart.setPic_url("http://img01.taotaosou.cn/size/image15/M08/06/4C/wKgDt1XulU0IAAAAAAB4pcvWN8sAAJ1zwFzjwIAAHi9198.jpg");
            cart.setStock(i);
            cart.setStore("hahahhahah");
            cart.setProductID(i + 100);
            cart.setPrice("100.30");
            cart.setProductName("爱吃爱吃");
            list2.add(cart);
        }
        list2.add(new ShoppingCart());
        child.put("天猫", list2);

        List<ShoppingCart> list3 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ShoppingCart cart = new ShoppingCart();
            cart.setPic_url("http://img01.taotaosou.cn/size/image15/M08/06/4C/wKgDt1XulU0IAAAAAAB4pcvWN8sAAJ1zwFzjwIAAHi9198.jpg");
            cart.setStock(i);
            cart.setStore("hahahhahah");
            cart.setProductID(i + 100);
            cart.setPrice("100.30");
            cart.setProductName("爱吃爱吃");
            list3.add(cart);
        }
        list3.add(new ShoppingCart());
        child.put("京东", list3);

        adapter.setParent(parent);
        adapter.setChild(child);

        //全部展开
        for (int i = 0; i < parent.size(); i++) {
            expandable_lv.expandGroup(i);
        }
        all_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                int size = parent.size();
                for (int i = 0; i <size ; i++)
                {
                    groupChecked(i,isChecked);
                }
                calculateCost();
            }
        });

    }

    @Override
    public void calculateCost()
    {
        countCost = 0;//归零重新计算
        int size = parent.size();
        for (int i = 0; i <size ; i++) {
            List<ShoppingCart> childs = child.get(parent.get(i).getStoreName());
            for (int j = 0; j < childs.size(); j++)
            {
                ShoppingCart cart = childs.get(j);
                if (cart.ischecked())
                {
                    countCost += cart.getSelectCount() * Float.parseFloat(cart.getPrice());
                }
            }
        }

    totalCost.setText(countCost+"");
    }

    @Override
    public void groupChecked(int groupId, boolean ischeck)
    {
        List<ShoppingCart> childs = child.get(parent.get(groupId).getStoreName());
        for (ShoppingCart cart : childs)
        {
            cart.setChecked(ischeck);
        }
        parent.get(groupId).setIsChecked(ischeck);
        adapter.notifyDataSetChanged();
    }
}
